home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Mail.php < prev    next >
PHP Script  |  2004-10-01  |  7KB  |  195 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Chuck Hagenbuch <chuck@horde.org>                            |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Mail.php,v 1.6 2003/06/26 07:05:36 jon Exp $
  20.  
  21. require_once 'PEAR.php';
  22.  
  23. /**
  24.  * PEAR's Mail:: interface. Defines the interface for implementing
  25.  * mailers under the PEAR hierarchy, and provides supporting functions
  26.  * useful in multiple mailer backends.
  27.  *
  28.  * @access public
  29.  * @version $Revision: 1.6 $
  30.  * @package Mail
  31.  */
  32. class Mail
  33. {
  34.     /**
  35.      * Line terminator used for separating header lines.
  36.      * @var string
  37.      */
  38.     var $sep = "\r\n";
  39.  
  40.     /**
  41.      * Provides an interface for generating Mail:: objects of various
  42.      * types
  43.      *
  44.      * @param string $driver The kind of Mail:: object to instantiate.
  45.      * @param array  $params The parameters to pass to the Mail:: object.
  46.      * @return object Mail a instance of the driver class or if fails a PEAR Error
  47.      * @access public
  48.      */
  49.     function factory($driver, $params = array())
  50.     {
  51.         $driver = strtolower($driver);
  52.         @include_once 'Mail/' . $driver . '.php';
  53.         $class = 'Mail_' . $driver;
  54.         if (class_exists($class)) {
  55.             return new $class($params);
  56.         } else {
  57.             return PEAR::raiseError('Unable to find class for driver ' . $driver);
  58.         }
  59.     }
  60.  
  61.     /**
  62.      * Implements Mail::send() function using php's built-in mail()
  63.      * command.
  64.      *
  65.      * @param mixed $recipients Either a comma-seperated list of recipients
  66.      *              (RFC822 compliant), or an array of recipients,
  67.      *              each RFC822 valid. This may contain recipients not
  68.      *              specified in the headers, for Bcc:, resending
  69.      *              messages, etc.
  70.      *
  71.      * @param array $headers The array of headers to send with the mail, in an
  72.      *              associative array, where the array key is the
  73.      *              header name (ie, 'Subject'), and the array value
  74.      *              is the header value (ie, 'test'). The header
  75.      *              produced from those values would be 'Subject:
  76.      *              test'.
  77.      *
  78.      * @param string $body The full text of the message body, including any
  79.      *               Mime parts, etc.
  80.      *
  81.      * @return mixed Returns true on success, or a PEAR_Error
  82.      *               containing a descriptive error message on
  83.      *               failure.
  84.      * @access public
  85.      * @deprecated use Mail_mail::send instead
  86.      */
  87.     function send($recipients, $headers, $body)
  88.     {
  89.         // if we're passed an array of recipients, implode it.
  90.         if (is_array($recipients)) {
  91.             $recipients = implode(', ', $recipients);
  92.         }
  93.  
  94.         // get the Subject out of the headers array so that we can
  95.         // pass it as a seperate argument to mail().
  96.         $subject = '';
  97.         if (isset($headers['Subject'])) {
  98.             $subject = $headers['Subject'];
  99.             unset($headers['Subject']);
  100.         }
  101.  
  102.         // flatten the headers out.
  103.         list(,$text_headers) = Mail::prepareHeaders($headers);
  104.  
  105.         return mail($recipients, $subject, $body, $text_headers);
  106.  
  107.     }
  108.  
  109.     /**
  110.      * Take an array of mail headers and return a string containing
  111.      * text usable in sending a message.
  112.      *
  113.      * @param array $headers The array of headers to prepare, in an associative
  114.      *              array, where the array key is the header name (ie,
  115.      *              'Subject'), and the array value is the header
  116.      *              value (ie, 'test'). The header produced from those
  117.      *              values would be 'Subject: test'.
  118.      *
  119.      * @return mixed Returns false if it encounters a bad address,
  120.      *               otherwise returns an array containing two
  121.      *               elements: Any From: address found in the headers,
  122.      *               and the plain text version of the headers.
  123.      * @access private
  124.      */
  125.     function prepareHeaders($headers)
  126.     {
  127.         $lines = array();
  128.         $from = null;
  129.  
  130.         foreach ($headers as $key => $value) {
  131.             if ($key === 'From') {
  132.                 include_once 'Mail/RFC822.php';
  133.  
  134.                 $addresses = Mail_RFC822::parseAddressList($value, 'localhost',
  135.                                                            false);
  136.                 $from = $addresses[0]->mailbox . '@' . $addresses[0]->host;
  137.  
  138.                 // Reject envelope From: addresses with spaces.
  139.                 if (strstr($from, ' ')) {
  140.                     return false;
  141.                 }
  142.  
  143.                 $lines[] = $key . ': ' . $value;
  144.             } elseif ($key === 'Received') {
  145.                 // Put Received: headers at the top.  Spam detectors often
  146.                 // flag messages with Received: headers after the Subject:
  147.                 // as spam.
  148.                 array_unshift($lines, $key . ': ' . $value);
  149.             } else {
  150.                 $lines[] = $key . ': ' . $value;
  151.             }
  152.         }
  153.  
  154.         return array($from, join($this->sep, $lines) . $this->sep);
  155.     }
  156.  
  157.     /**
  158.      * Take a set of recipients and parse them, returning an array of
  159.      * bare addresses (forward paths) that can be passed to sendmail
  160.      * or an smtp server with the rcpt to: command.
  161.      *
  162.      * @param mixed Either a comma-seperated list of recipients
  163.      *              (RFC822 compliant), or an array of recipients,
  164.      *              each RFC822 valid.
  165.      *
  166.      * @return array An array of forward paths (bare addresses).
  167.      * @access private
  168.      */
  169.     function parseRecipients($recipients)
  170.     {
  171.         include_once 'Mail/RFC822.php';
  172.  
  173.         // if we're passed an array, assume addresses are valid and
  174.         // implode them before parsing.
  175.         if (is_array($recipients)) {
  176.             $recipients = implode(', ', $recipients);
  177.         }
  178.  
  179.         // Parse recipients, leaving out all personal info. This is
  180.         // for smtp recipients, etc. All relevant personal information
  181.         // should already be in the headers.
  182.         $addresses = Mail_RFC822::parseAddressList($recipients, 'localhost', false);
  183.         $recipients = array();
  184.         if (is_array($addresses)) {
  185.             foreach ($addresses as $ob) {
  186.                 $recipients[] = $ob->mailbox . '@' . $ob->host;
  187.             }
  188.         }
  189.  
  190.         return $recipients;
  191.     }
  192.  
  193. }
  194. ?>
  195.